﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _8_1_Gather
{
    public abstract class  Animal 
    {
        protected string name;

        public string Name
        {
            set { name = value; }
            get { return name; }
        }

        public Animal()
        {
            name = "这个动物没有名字";
        }

        public Animal(string newName)
        {
            name = newName;
        }
    }

    //奶牛
    public class Cow : Animal
    {
        public void Milk()
        {
            Console.WriteLine("{0} 在产奶。", Name);
        }

        public Cow(string newName) : base(newName)
        {

        }
    }
    //鸡
    public class Chicken : Animal
    {
        public void LayEgg()
        {
            Console.WriteLine("{0} 在下蛋。", Name);
        }

        public Chicken(string newName) : base(newName)
        {

        }
    }

    //可枚举
    public class Animals1 : IEnumerable //: CollectionBase
    {
        private int MaxLength = 4;
        private int Length = 0;
        private Animal[] List;

        public Animals1()
        {
            List = new Animal[MaxLength];
        }
        public void Add(Animal animal)
        {
            if (Length == MaxLength)
            {
                MaxLength = Length * 2;
                Animal[] t = new Animal[MaxLength];
                List.CopyTo(t, 0);
                List = t;
            }
            List[Length] = animal;
            Length++;
        }

        public void Remove(Animal animal)
        {
            bool flag = false;
            for (int i = 0; i < Length; i++)
            {
                if (List[i] == animal)
                {
                    flag = true;
                }
                if (flag)
                    List[i] = List[i + 1];
            };
            if (flag)
                Length--;

        }

        public void RemoveAt(int index)
        {
            if (index < 0 || index >= Length)
                return;
            Remove(List[index]);
        }

        //支持foreach
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ( IEnumerator )GetEnumerator();
        }

        public AnimalEnum GetEnumerator()
        {
            Animal[] temp = new Animal[Length];
            Array.Copy(List, temp, Length);
            return new AnimalEnum(temp);
        }

        //索引器
        public Animal this[int index]
        {
            set { List[index] = value; }
            get { return (Animal)List[index]; }
        }


        public int MaxCount
        {
            get { return List.Length; }
        }

        public int Count
        {
            get { return Length; }
        }

    }

    //枚举器
    public class AnimalEnum : IEnumerator
    {
        public Animal[] _animals;
        private int position = -1;

        public AnimalEnum(Animal[] list)
        {
            _animals = list;
        }
        object IEnumerator.Current
        {
            get
            {
                return Current;
            }
        }

        public Animal Current
        {
            get
            {
                try
                {
                    return _animals[position];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }



        public bool MoveNext()
        {
            position++;
            return (position < _animals.Length);
        }

        public void Reset()
        {
            position = -1;
        }
    }

    public class Animals : IEnumerable //: CollectionBase
    {
        
        private ArrayList List = new ArrayList();
        public void Add(Animal animal)
        {
            List.Add(animal);
        }

        public void Remove(Animal animal)
        {
            List.Remove(animal);
        }

        public void RemoveAt(int index)
        {
            List.RemoveAt(index);
        }

        //支持foreach
        public IEnumerator GetEnumerator()
        {
            return List.GetEnumerator();
        }

        //索引器
        public Animal this[int index]
        {
            set { List[index] = value; }
            get { return (Animal)List[index]; }
        }

        
        public int Count
        {
            get { return List.Count; }
        }

    }

    public class Animals2 : CollectionBase
    {
        public void Add(Animal animal)
        {
            List.Add(animal);
        }

        public void Remove(Animal animal)
        {
            List.Remove(animal);
        }
        //索引器
        public Animal this[int index]
        {
            set { List[index] = value; }
            get { return (Animal)List[index]; }
        }

    }

    public class AnimalTest
    {
        public static void Test()
        {

            Dictionary<int, Animal> animalDic1 = new Dictionary<int, Animal>();
            animalDic1.Add(100, new Cow("Cow1111"));
            animalDic1.Add(2, new Chicken("Chicken2222"));
            animalDic1.Add(9, new Chicken("Chicken4444"));
            animalDic1.Add(77, new Cow("Cow2222"));
            animalDic1.Add(11, new Chicken("Chicken5555"));

            //Console.WriteLine(animalDic1[100].Name);
            animalDic1[5] = new Chicken("aaaa");


            foreach (int item in animalDic1.Keys)
            {

            }

            foreach (Animal item in animalDic1.Values)
            {

            }
            Animal a;
            if (animalDic1.TryGetValue(100,out a))
            {
                Console.WriteLine(a.Name);
            }

            animalDic1.Remove(100);

            if (animalDic1.TryGetValue(100, out a))
            {
                Console.WriteLine(a.Name);
            }
            else
                Console.WriteLine("Not Find");

            Console.ReadKey();

            /*
            for (int i = animalDic1.Count; i >0 ; i--)
            {
                //animalDic1.
            }
            */

            List<int> removeKeys = new List<int>(); 
            foreach (var item in animalDic1)
            {
                if (item.Value.GetType() == typeof(Cow))
                {
                    //animalDic1.Remove(item.Key)
                    removeKeys.Add(item.Key);
                }
            }

            foreach (var item in removeKeys)
            {
                animalDic1.Remove(item);
            }

            Console.ReadKey();

            Dictionary<string, Animal> animalDic = new Dictionary<string, Animal>();
            animalDic.Add("Cow", new Cow("Cow1111"));
            animalDic.Add("Chicken", new Chicken("Chicken2222"));

            Console.WriteLine(animalDic["Cow"].Name);

            Console.ReadKey();

            foreach (var item in animalDic)
            {
                Console.WriteLine("{0} 动物的名字是：{1}", item.Value.ToString(), item.Value.Name);

            }

            foreach (KeyValuePair<string, Animal> kvp in animalDic)
            {
                Console.WriteLine("{0} 动物的名字是：{1}", kvp.Value.ToString(), kvp.Value.Name);
            }

            List<Animal> list = new List<Animal>();
            list.Add(new Cow("我的奶牛"));
            list.Add(new Chicken("我的鸡"));

            foreach (Animal animal in list)
            {
                Console.WriteLine("{0} 动物的名字是：{1}", animal.ToString(), animal.Name);
            }

            list[0].Name = "asdfg";
            ((Cow)list[0]).Milk();

            Console.WriteLine(list[0].Name);

            Console.ReadKey();

            Animals2 animals = new Animals2();
            animals.Add(new Cow("我的奶牛"));
            animals.Add(new Chicken("我的鸡"));
            ((Cow)animals[0]).Milk();
            ((Chicken)animals[1]).LayEgg();

            foreach (Animal animal in animals)
            {
                Console.WriteLine("{0} 动物的名字是：{1}", animal.ToString(), animal.Name);
            }

            Console.ReadKey();
            Console.Clear();

            for (int i = 0; i < animals.Count; i++)
            {
                Animal animal = animals[i];
                Console.WriteLine("{0} 动物的名字是：{1}", animal.ToString(), animal.Name);
            }

            Console.ReadKey();

            Animal[] animalArray = new Animal[2];
            animalArray[0] = new Cow("我的奶牛");
            animalArray[1] = new Chicken("我的鸡");

            foreach (Animal animal in animalArray)
            {
                Console.WriteLine("{0} 动物的名字是：{1}", animal.ToString(), animal.Name);
            }

            ((Cow)animalArray[0]).Milk();
            ((Chicken)animalArray[1]).LayEgg();

            ArrayList animalArrayList = new ArrayList();
            animalArrayList.Add(new Cow("我的奶牛"));
            animalArrayList.Add(new Chicken("我的鸡"));
            animalArrayList.Add(new Cow("我的另一只奶牛"));

            foreach (Animal animal in animalArrayList)
            {
                Console.WriteLine("{0} 动物的名字是：{1}", animal.ToString(), animal.Name);
            }
            ((Cow)animalArrayList[0]).Milk();
            ((Chicken)animalArrayList[1]).LayEgg();
            ((Cow)animalArrayList[2]).Milk();

            animalArrayList.RemoveAt(0);

            foreach (Animal animal in animalArrayList)
            {
                Console.WriteLine("{0} 动物的名字是：{1}", animal.ToString(), animal.Name);
            }
        }
    }
}
